package com.scpii.universal.pns.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.CatelogDao;
import com.scpii.universal.hibernate.dao.ContentDao;
import com.scpii.universal.hibernate.entity.CatelogTab;
import com.scpii.universal.hibernate.entity.ContentTab;
import com.scpii.universal.pns.dao.ActivitiesDao;
import com.scpii.universal.pns.dao.UserActionsDtoDao;
import com.scpii.universal.pns.dto.ActivitiesJoinDto;
import com.scpii.universal.pns.dto.UsersActionDto;
import com.scpii.universal.pns.entity.Activities;

@Service
public class ActivitiesServiceImpl extends PnsBaseServiceImpl<Activities, Long>
		implements ActivitiesService {
	@Resource
	private ActivitiesDao activitiesDao;
	@Resource
	private ContentDao contentDao;
	@Resource
	private UserActionsDtoDao userActionsDtoDao;
	@Resource
	private CatelogDao catelogDao;

	@Resource
	public void setBaseDao(ActivitiesDao activitiesDao) {
		super.setBaseDao(activitiesDao);
	}

	public List<ActivitiesJoinDto> getActivitidesInit(Integer appId,
			Integer pageNo, Integer pageSize) {
		// String sql = ";
		// List<CatelogTab> cateList = catelogDao.findByHql(sql, 0, 0, null);
		// Integer intCateId = 0;
		// if (cateList != null && cateList.size() > 0) {
		// for (CatelogTab cate : cateList) {
		// if (cate.getIntHasSubCate() == 0) {
		// intCateId = cate.getIntCateId();
		// }
		// }
		//
		// }
		StringBuffer hql = new StringBuffer("select t from ContentTab  t ");
		hql.append(" where t.intAppId = :appId and exists(select t from CatelogTab t1 where t1.intAppId="
				+ appId
				+ " and t1.intType=4 and t1. intHasSubCate=0 and t1.intCateId=t.intCateId) ");
		hql.append(" and t.intDeleteSign= :intDeleteSign order by t.id desc");
		Map<String, Object> map = new HashMap<String, Object>();
		// TODO map.put("appId", getAppId(enterId));
		map.put("appId", appId);
		// map.put("intCateId", intCateId);
		map.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<ContentTab> list = contentDao.findByHql(hql.toString(), pageNo,
				pageSize, map);
		List<ActivitiesJoinDto> actiList = null;
		if (list != null && list.size() > 0) {
			actiList = new ArrayList<ActivitiesJoinDto>();
			ActivitiesJoinDto joinDto = null;
			// 动作类型 COMMENTS_ADD ACTIVITES_FORWARD 活动转发
			// 评论添加 ACTIVITES_SHARE 分享 ACTIVITES_JOIN 活动参加 ACTIVITES_FAV 活动感兴趣
			for (ContentTab content : list) {
				Long targetId = 480000000000L + Long.parseLong(content
						.getIntContentId().toString());
				joinDto = new ActivitiesJoinDto();
				joinDto.setActiId(content.getIntContentId());
				joinDto.setActiName(content.getVarContentName());
				joinDto.setSignTime(content.getDatAddDatetime());
				joinDto.setCommentUserCount(getCountByUserJoinType(targetId,
						GenericConstants.USER_ACTIONS_COMMENTS_ADD));
				joinDto.setFavoritesUserCount(getCountByUserJoinType(targetId,
						GenericConstants.USER_ACTIONS_ACTIVITES_FAV));
				joinDto.setShareUserCount(getCountByUserJoinType(targetId,
						GenericConstants.USER_ACTIONS_ACTIVITES_SHARE));
				joinDto.setJoinUserCount(getCountByUserJoinType(targetId,
						GenericConstants.USER_ACTIONS_ACTIVITES_JOIN));
				actiList.add(joinDto);
			}

		}

		return actiList;
	}

	public Integer getCountByUserJoinType(Long targetId, String joinType) {

		StringBuffer hql = new StringBuffer(
				"select count(*) from  com.scpii.universal.pns.entity.UserActions t ");
		hql.append(" where t.targetId = :targetId ");
		hql.append(" and t.actionType = :actionType ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("targetId", targetId);
		map.put("actionType", joinType);
		List<Long> list = userActionsDtoDao.findByHqlCount(hql.toString(), 0,
				0, map, null);
		if (list != null && list.size() == 1) {
			return list.get(0).intValue();
		} else {
			return 0;
		}

	}

	public Integer getActivitidesInitCount(Integer appId) {
		StringBuffer hql = new StringBuffer("select t from ContentTab  t ");
		hql.append(" where t.intAppId = :appId and exists(select t from CatelogTab t1 where t1.intAppId="
				+ appId
				+ " and t1.intType=4 and t1. intHasSubCate=0 and t1.intCateId=t.intCateId) ");
		hql.append(" and t.intDeleteSign= :intDeleteSign order by t.id desc");
		Map<String, Object> map = new HashMap<String, Object>();
		// TODO map.put("appId", getAppId(enterId));
		map.put("appId", appId);
		// map.put("intCateId", intCateId);
		map.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<ContentTab> list = contentDao.findByHql(hql.toString(), 0, 0, map);
		if (list != null && list.size() > 0) {
			return list.size();
		} else {
			return 0;
		}

	}

	/**
	 * 用户动作管理
	 * 
	 * @param targetId
	 * @param actionType
	 * @return
	 */
	public Integer getUserActionCount(Long targetId, String actionType) {
		StringBuffer hql = new StringBuffer("select count(*) ");
		hql.append(" from Users u,UserActions ua");
		hql.append(" where ua.userId= u.id ");
		hql.append(" and ua.targetId = :targetId ");
		hql.append(" and ua.actionType = :actionType ");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("targetId", 480000000000L + targetId);
		map.put("actionType", actionType);
		List<Long> list = userActionsDtoDao.findByHqlCount(hql.toString(), 0,
				0, map, null);
		if (list != null && list.size() == 1) {
			return list.get(0).intValue();
		} else {
			return 0;
		}

	}

	/**
	 * 用户动作管理
	 * 
	 * @param targetId
	 * @param actionType
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public List<UsersActionDto> getUserAction(Long targetId, String actionType,
			Integer pageNo, Integer pageSize) {

		StringBuffer hql = new StringBuffer(
				"select new  com.scpii.universal.pns.dto.UsersActionDto( ");
		hql.append(" ua.targetId,");
		hql.append(" ua.actionType,");
		hql.append(" ua.createDt,");
		hql.append(" u.userName,");
		hql.append(" u.phoneNumber,");
		hql.append(" u.userAilas )");
		hql.append(" from Users u,UserActions ua");
		hql.append(" where ua.userId= u.id ");
		hql.append(" and ua.targetId = :targetId ");
		hql.append(" and ua.actionType = :actionType ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("targetId", 480000000000L + targetId);
		map.put("actionType", actionType);
		List<UsersActionDto> list = userActionsDtoDao.findByHql(hql.toString(),
				pageNo, pageSize, map, null);
		return list;

	}
}
